<template>
  <div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Author',
  data () {
    return {
      webUrl: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx421b7ae4ca04f40e&redirect_uri=http%3A%2F%2Fmall.meimai94buy.com&response_type=code&scope=snsapi_userinfo'
    }
  },
  methods: {
    isWeixinBrowser () {
      let ua = navigator.userAgent.toLowerCase()
      return !!(/micromessenger/.test(ua))
    },
    login () {
      let code = sessionStorage.getItem('wechatCode')
      let personalCode = sessionStorage.getItem('wechatState')
      // 通过cookie中保存的token 获取用户信息
      axios.get('/meimai/interface/wx/accessCode', {
        params: {
          code: code,
          personalCode: personalCode
        }
      }).then((res) => {
        // alert('1' + res)
        // alert('2' + res.code)
        // alert('3' + res.data.code)
        sessionStorage.removeItem('user')
        if (res.data.code === 0) {
          this.$store.commit('ADD_COUNT', res.data)
          // this.$router.push(this.$route.query.redirect)
          sessionStorage.removeItem('beforeLoginUrl')
          sessionStorage.removeItem('shareId')
          if (sessionStorage.getItem('DetailPath')) {
            let detailPath = sessionStorage.getItem('DetailPath')
            sessionStorage.removeItem('DetailPath')
            this.$router.push(detailPath)
          } else {
            this.$router.push('/')
          }
        } else {
          // window.location.href = this.webUrl + '&state=' + personalCode + '#wechat_redirect'
        }
      })
    }
  },
  created () {
    // 检测会员有没有登录
    if (!sessionStorage.getItem('user')) {
      sessionStorage.setItem('user', true)
      if (this.isWeixinBrowser()) {
        let state = ''
        if (sessionStorage.getItem('shareId')) {
          state = '&state=' + sessionStorage.getItem('shareId')
        }
        // console.log(this.webUrl + state + '#wechat_redirect')
        // alert(this.webUrl + state + '#wechat_redirect')
        window.location.href = this.webUrl + state + '#wechat_redirect'
      } else {
        sessionStorage.removeItem('user')
        this.$router.push('/login')
        // if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        //   // Ios
        // } else if (/(Android)/i.test(navigator.userAgent)) {
        //   // Android终端
        // }
      }
    } else {
      // 如果有token 但是vuex中没有用户登录信息则做登录操作
      this.login()
      // alert(222)
    }
  }
}
</script>

<style lang="stylus" scoped>
</style>
